package routers

import (
	beego "github.com/beego/beego/v2/server/web"
	"github.com/beego/beego/v2/server/web/context"
	"shang/conmon"
	"shang/controllers"
	"strconv"
)

func init() {
	beego.Router("/", &controllers.MainController{})
	beego.InsertFilter("/ws/*", beego.BeforeRouter, filterFunc)
	//todo: 注册
	beego.Router("/register", &controllers.RegController{}, "post:Reg")
	//todo: 登录
	beego.Router("/login", &controllers.LoginController{}, "post:Login")
	//todo: 聊天
	beego.Router("/ws/im", &controllers.ImController{}, "get:Im")
	//todo: 生产者
	beego.Router("/publish", &controllers.RabbitController{}, "post:Publish")
	//todo: 消费者1
	beego.Router("/simple_one", &controllers.RabbitController{}, "post:SimpleOne")
	//todo: 消费者2
	beego.Router("/simple_two", &controllers.RabbitController{}, "post:SimpleTwo")
	//todo: 添加好友
	beego.Router("/friend", &controllers.FriendController{}, "post:Friend")
	//todo: 弹幕发送
	beego.Router("/create_barrage", &controllers.BarrageController{}, "get:CreateBarrage")
	//todo: 弹幕查询
	beego.Router("/show_barrage", &controllers.BarrageController{}, "get:ShowBarrage")
	//todo: oss文件上传
	beego.Router("/Upload", &controllers.UploadController{}, "post:Upload")
	//todo: 排行榜(频道)电影/电视剧/动漫
	beego.Router("/video_channel_top", &controllers.ChannelTopController{}, "post:ChannelTop")
	//todo: 排行榜(类型)爱情/古装/悬疑/惊悚
	beego.Router("/video_type_top", &controllers.TypeTopController{}, "post:TypeTop")
	//todo: 创建直播间
	beego.Router("/create_broadcast", &controllers.BroadcastController{}, "post:CreateBroadcast")
	//todo: Look直播
	beego.Router("/look_broadcast", &controllers.BroadcastController{}, "post:LookBroadcast")
	//todo: 加入黑名单
	beego.Router("/append_blacklist", &controllers.AppendBlacklistController{}, "post:AppendBlacklist")
}

func filterFunc(ctx *context.Context) {
	token := ctx.Request.Header.Get("token")
	if token == "" {
		ctx.Output.JSON(map[string]interface{}{
			"code": 400,
			"msg":  "token不能为空",
		}, true, false)
		return
	}
	payload, err := conmon.ParseToken(token)
	if err != nil {
		ctx.Output.JSON(map[string]interface{}{
			"code": 400,
			"msg":  err.Error(),
		}, true, false)
		return
	}
	id, _ := strconv.Atoi(payload)
	conmon.Userid = id
}
